<?php

namespace addons\zzyy\controller\api;


use app\admin\model\zzyy\ZzyyRechargeSetting;
use app\admin\model\zzyy\ZzyyUserBalance;
use app\admin\model\zzyy\ZzyyUserExtend;

class Recharge extends \app\common\controller\Api
{
    protected $noNeedLogin = ['*'];


    public function options()
    {
        $options = ZzyyRechargeSetting::order('num', 'asc')->select();
        $this->success('ok', $options);
    }

    public function updateBalance()
    {
        $user = $this->auth->getUser();
        $rechargeKey = $this->request->post('recharge_id');

        $rechargeSetting = ZzyyRechargeSetting::get($rechargeKey);

        $transactionId = $this->request->post('transaction_id');


        $userExtend = ZzyyUserExtend::where('user_id', $user->id)->find();

        $addNum = $rechargeSetting->num + $rechargeSetting->extra;
        $afterValue = $userExtend->balance + $addNum;

        ZzyyUserBalance::create([
            'user_id' => $user->id,
            'type' => ZzyyUserBalance::TYPE_RECHARGE, //1=充值
            'value' => $addNum,
            'before_value' => $userExtend->balance,
            'after_value' => $afterValue,
            'transaction_id' => $transactionId,
            'createtime' => time(),
            'updatetime' => time()
        ]);

        $userExtend->balance = $afterValue;
        $userExtend->save();

        $this->success('用户余额更新成功');
    }

}